<?php
	include_once "classes/loainguoidung_bs.php";
	include_once "classes/nguoidung_bs.php";
	include_once "classes/history_bs.php";
	
	$TenDangNhap = $_POST['TenDangNhap'];
	$MatKhau     = $_POST['MatKhau'];
	
	$nguoidung = new nguoidung_bs();
	$nguoidung->SetTenDangNhap($TenDangNhap);
	
	$record = $nguoidung->LayDanhSach();
	
	if($TenDangNhap == $record[0]['TenDangNhap'] && md5($MatKhau) == $record[0]['MatKhau'])
	{
		$tmp = $record[0]['MaLoaiNguoiDung'] * 1;
		
		$loainguoidung = new loainguoidung_bs();
		$loainguoidung->SetMaLoaiNguoiDung($tmp);
		
		$record_lnd = $loainguoidung->LayDanhSach();
		
		$_SESSION['DangNhap'] = 1;
		$_SESSION['TenDangNhap']       = $TenDangNhap;
		$_SESSION['ThongTinNguoiDung'] = $record[0];
		$_SESSION['HoTen']             = $record[0]['HoTen'];
		$_SESSION['MaND']              = $record[0]['MaNguoiDung'];
		$_SESSION['Avatar'] 		   = $record[0]['Avatar'];
		$_SESSION['MaLoaiNguoiDung']   = $tmp;
		$_SESSION['TenLoaiNguoiDung']  = $record_lnd[0]['TenLoaiNguoiDung'];
		
		
		//Ghi lịch sử
		$ls = new history_bs();
		$ls->SetTenDangNhap($TenDangNhap);
		date_default_timezone_set('Asia/Ho_Chi_Minh');
		$ls->SetThoiGian(date('y-m-d h:i:s a', time()));
		$kq = $ls->Them();
		
		header("Location: index.php");
	}
	else
	{	
		$_SESSION['ThongBao'] = "Sai thông tin đăng nhập.";
		header("Location: index.php?action=component/thongbao");
	}
?>